<!--
功能：登陆组件,
作者：RanSheng,
邮箱：1534677527@qq.com",
时间：2021/5/30 0:25,
版本：v1.0,
修改记录：,
修改内容：,
修改人员：,
修改时间：,
-->
<template>
  <div>
    <div class="login-box">
      <div class="login">
        <div class="imag" @click="$router.push('/home/welcome')">
          <el-image :src="url" class="imags" fit="fill">
          </el-image>
        </div>
        <div class="from">
          <el-form ref="form"
                   status-icon
                   :rules="rules"
                   :model="form"
                   class="from-info"
                   label-width="0px">
            <el-form-item prop="name">
              <el-input v-model="form.name"
                        placeholder="请输入账号">
                <template slot="prepend">账号</template>
                <i slot="prefix"
                   class="el-input__icon el-icon-s-custom"></i>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input show-password
                        placeholder="请输入密码"
                        v-model="form.password"
                        @keyup.enter.native="submitForm">
                <template slot="prepend">密码</template>
                <i slot="prefix"
                   class="el-input__icon el-icon-lock"></i>
              </el-input>
            </el-form-item>
            <el-form-item class="btn">
              <el-button @click="submitForm()" type="primary">快速登录</el-button>
              <span @click="$router.push('/forget')">忘记密码？</span>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {setCookie} from "@/utils/CookieUtils";
import {confirm, message} from '../utils/age'
import {mapState} from 'vuex'
export default {
  name: 'login',
  data() {
    return {
      url: 'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
      error: 404,
      token: 123,
      form: {
        name: '',
        password: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '账号在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '密码在 3 到 10 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
    this.form.name=this.username
  },
  computed:{
    ...mapState(["username"])
  },
  methods: {
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 请求登录
          this.$http.post('login', this.form).then(response => {
            console.log(response)
            if (response.data) {
              setCookie("token", response.data, 1)
              this.$store.commit("setUsername",this.form.name)
              this.form = {}
              this.$store.commit("setUsr",{})
              this.$router.push('/home/welcome')
            } else {
              message("error", "账号或密码错误")
            }
          }).catch(err => {
            message("error", "账号或密码错误")
          })
        } else {
          message("warning", "请正确输入账号和密码！")
        }
      });
    },
  },
  watch:{
  }
}
</script>

<style scoped>
.from {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.imags {
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 50%;
}

.from-info {
  position: absolute;
  top: 70px;
  width: 70%;
  padding: 5px 30px;
  box-sizing: border-box;
}

.btn {
  text-align: center;
  position: relative;
  width: 100%;
}

.btn span {
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
  transition: all .3s;
}

.btn span:hover {
  color: #db2f00;
}

.login .imag img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login .imag {
  cursor: pointer;
  width: 80px;
  height: 80px;
  background-color: #fff;
  border-radius: 50%;
  position: absolute;
  border: 2px solid #eeeeee;
  box-shadow: 0 0 2px #ddd;
  padding: 5px;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login {
  background-color: #ffffff;
  border-radius: 15px;
  width: 600px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login-box {
  position: relative;
  height: 730px;
  background-color: #F4F4F4;
}
</style>
